package com.lktx.bbfactory.controller;

import com.lktx.bbfactory.common.CommonPage;
import com.lktx.bbfactory.common.CommonResult;
import com.lktx.bbfactory.entiy.DTO.LoginDto;
import com.lktx.bbfactory.entiy.User;
import com.lktx.bbfactory.service.userService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private userService userService;

    //分页查询
    @GetMapping("/pageList")
    public CommonResult<CommonPage<User>> pageList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String name) {
        CommonPage<User> page = userService.pageUser(pageNum, pageSize, username, name);
        return CommonResult.success(page);
    }

    //用户登录
    @PostMapping("/login")
    public CommonResult<String> login(@RequestBody LoginDto dto) {
        String token = userService.login(dto.getUsername(), dto.getPassword());
        return CommonResult.success(dto.getUsername());
    }



    // 新增用户
    @PostMapping("/add")
    public CommonResult<String> addUser(@RequestBody User user) {
        userService.addUser(user);
        return CommonResult.success("添加成功");
    }


    // 更新用户
    @PostMapping("/update")
    public CommonResult<String> updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return CommonResult.success("更新成功");
    }


    // 删除用户
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> deleteUser(@PathVariable Long id) {
         userService.deleteUser(id);
        return CommonResult.success("删除成功");
    }


}
